<html>
<head><meta charset="utf-8"><title>Should we have something like `final` for trait methods? · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html">Should we have something like `final` for trait methods?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223158363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223158363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223158363">(Jan 18 2021 at 19:51)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/81157">https://github.com/rust-lang/rust/issues/81157</a> is an interesting example of where it would be nice to have a helper method on a trait, but that makes it unreliable because people can implement it wrong -- and the workarounds of making it a function (or inherent method) is kinda annoying.</p>
<p>(Reminds me of the "sealed traits" concept too.)</p>



<a name="223161967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223161967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223161967">(Jan 18 2021 at 20:39)</a>:</h4>
<p>I've totally wanted this! A messier way to achieve it would be to have:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">MyTrait</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">final_method</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="fm">todo!</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="223162598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223162598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223162598">(Jan 18 2021 at 20:48)</a>:</h4>
<p>there are some traits in bytemuck that have this, with part of the <code>unsafe trait</code> implementation contract being that you don't override that method.</p>



<a name="223165734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223165734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223165734">(Jan 18 2021 at 21:38)</a>:</h4>
<p>It would also be handy to have <code>final</code> for associated constants and types, not just functions.</p>



<a name="223166981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223166981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223166981">(Jan 18 2021 at 21:59)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Foo</span>: <span class="nc">FooHelper</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">FooHelper</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="n">FooHelper</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>A user could implement <code>FooHelper</code> for other types, but those can't also be <code>Foo</code></p>



<a name="223176141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223176141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223176141">(Jan 19 2021 at 00:33)</a>:</h4>
<p>Specialization makes me worry that <code>FooHelper</code> isn't reliable forever either...</p>



<a name="223181288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223181288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223181288">(Jan 19 2021 at 02:23)</a>:</h4>
<p>is there a future where specialization is possible without explicitly opting in? (<code>default impl</code>/<code>default fn</code>)</p>



<a name="223181814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223181814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223181814">(Jan 19 2021 at 02:35)</a>:</h4>
<p>yeah i feel like this is the kind of thing i'd be worried about breaking in the face of future language features.</p>



<a name="223181858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/223181858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#223181858">(Jan 19 2021 at 02:36)</a>:</h4>
<p>that said its pretty clever</p>



<a name="224072788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/224072788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#224072788">(Jan 26 2021 at 17:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F/near/223181288">said</a>:</p>
<blockquote>
<p>is there a future where specialization is possible without explicitly opting in? (<code>default impl</code>/<code>default fn</code>)</p>
</blockquote>
<p>the original design had <code>partial impl</code> instead of <code>default impl</code>-- this would address this use case as well</p>



<a name="224072805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Should%20we%20have%20something%20like%20%60final%60%20for%20trait%20methods%3F/near/224072805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Should.20we.20have.20something.20like.20.60final.60.20for.20trait.20methods.3F.html#224072805">(Jan 26 2021 at 17:24)</a>:</h4>
<p>the idea was that you could write a <code>partial impl</code> with some items not defaulted, which then could not be specialized</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>